import pandas as pd
import numpy as np
import os
import math as m

# 函数输入为订单的start_time,返回data_day，data_day中包含了data中开始时间包含start_time的所有订单
def data_day(data, start_time):
    data_day = data[data['start_time'].str.contains(start_time)]
    return data_day

# 将每天的数据分别保存到data_day1,data_day2,data_day3,data_day4,data_day5,data_day6,data_day7，一直到data_day31中
def get_date_divided_data(data):
    data_day1 = data_day(data, '2016-08-01')
    data_day2 = data_day(data, '2016-08-02')
    data_day3 = data_day(data, '2016-08-03')
    data_day4 = data_day(data, '2016-08-04')
    data_day5 = data_day(data, '2016-08-05')
    data_day6 = data_day(data, '2016-08-06')
    data_day7 = data_day(data, '2016-08-07')
    data_day8 = data_day(data, '2016-08-08')
    data_day9 = data_day(data, '2016-08-09')
    data_day10 = data_day(data, '2016-08-10')
    data_day11 = data_day(data, '2016-08-11')
    data_day12 = data_day(data, '2016-08-12')
    data_day13 = data_day(data, '2016-08-13')
    data_day14 = data_day(data, '2016-08-14')
    data_day15 = data_day(data, '2016-08-15')
    data_day16 = data_day(data, '2016-08-16')
    data_day17 = data_day(data, '2016-08-17')
    data_day18 = data_day(data, '2016-08-18')
    data_day19 = data_day(data, '2016-08-19')
    data_day20 = data_day(data, '2016-08-20')
    data_day21 = data_day(data, '2016-08-21')
    data_day22 = data_day(data, '2016-08-22')
    data_day23 = data_day(data, '2016-08-23')
    data_day24 = data_day(data, '2016-08-24')
    data_day25 = data_day(data, '2016-08-25')
    data_day26 = data_day(data, '2016-08-26')
    data_day27 = data_day(data, '2016-08-27')
    data_day28 = data_day(data, '2016-08-28')
    data_day29 = data_day(data, '2016-08-29')
    data_day30 = data_day(data, '2016-08-30')
    data_day31 = data_day(data, '2016-08-31')
    data_day32 = data_day(data, '2016-09-01')
    data_day1.to_csv('processed_data/data_day1.csv', index=False)
    data_day2.to_csv('processed_data/data_day2.csv', index=False)
    data_day3.to_csv('processed_data/data_day3.csv', index=False)
    data_day4.to_csv('processed_data/data_day4.csv', index=False)
    data_day5.to_csv('processed_data/data_day5.csv', index=False)
    data_day6.to_csv('processed_data/data_day6.csv', index=False)
    data_day7.to_csv('processed_data/data_day7.csv', index=False)
    data_day8.to_csv('processed_data/data_day8.csv', index=False)
    data_day9.to_csv('processed_data/data_day9.csv', index=False)
    data_day10.to_csv('processed_data/data_day10.csv', index=False)
    data_day11.to_csv('processed_data/data_day11.csv', index=False)
    data_day12.to_csv('processed_data/data_day12.csv', index=False)
    data_day13.to_csv('processed_data/data_day13.csv', index=False)
    data_day14.to_csv('processed_data/data_day14.csv', index=False)
    data_day15.to_csv('processed_data/data_day15.csv', index=False)
    data_day16.to_csv('processed_data/data_day16.csv', index=False)
    data_day17.to_csv('processed_data/data_day17.csv', index=False)
    data_day18.to_csv('processed_data/data_day18.csv', index=False)
    data_day19.to_csv('processed_data/data_day19.csv', index=False)
    data_day20.to_csv('processed_data/data_day20.csv', index=False)
    data_day21.to_csv('processed_data/data_day21.csv', index=False)
    data_day22.to_csv('processed_data/data_day22.csv', index=False)
    data_day23.to_csv('processed_data/data_day23.csv', index=False)
    data_day24.to_csv('processed_data/data_day24.csv', index=False)
    data_day25.to_csv('processed_data/data_day25.csv', index=False)
    data_day26.to_csv('processed_data/data_day26.csv', index=False)
    data_day27.to_csv('processed_data/data_day27.csv', index=False)
    data_day28.to_csv('processed_data/data_day28.csv', index=False)
    data_day29.to_csv('processed_data/data_day29.csv', index=False)
    data_day30.to_csv('processed_data/data_day30.csv', index=False)
    data_day31.to_csv('processed_data/data_day31.csv', index=False)


# 输入为csv文件的路径，将该文件按照start_time升序排列，并保存在原文件中
def sort_csv(file_path):
    data = pd.read_csv(file_path)
    data = data.sort_values(by=['start_time'])
    data.to_csv(file_path, index=False)


# 输入为文件夹路径，将该文件夹下所有csv文件按照start_time升序排列，并保存在原文件夹中
def sort_csv_in_folder(folder_path):
    for file in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file)
        sort_csv(file_path)


# 输入为csv文件的路径，start_time为开始时间，end_time为结束时间，将该文件中start_time在start_time和end_time之间的数据保存在新的csv文件中,
def data_in_time_range(file_path, start_time, end_time):
    data = pd.read_csv(file_path)
    year_month_day = data['start_time'].str.split(' ', expand=True)[0]
    start_hour = start_time.split(':')[0]
    end_hour = end_time.split(':')[0]
    start_time = year_month_day + ' ' + start_time
    end_time = year_month_day + ' ' + end_time
    data = data[(data['start_time'] >= start_time) & (data['start_time'] <= end_time)]
    new_file_name = file_path.split('/')[-1].split('.')[0] + '_' + start_hour + '_' + end_hour + '.csv'
    data.to_csv(new_file_name, index=False)


# 输入为路径，把该路径下所有csv文件中start_time在00:00到06:00,06:00到12:00,12:00到18:00,18:00到24:00的数据保存在新的csv文件中,并删除原文件
def data_slice_in_folder(folder_path):
    for file in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file)
        data_in_time_range(file_path, '00:00', '06:00')
        data_in_time_range(file_path, '06:00', '12:00')
        data_in_time_range(file_path, '12:00', '18:00')
        data_in_time_range(file_path, '18:00', '23:59')
        os.remove(file_path)

# # 传入为文件夹路径，location_x,y的最大最小值，网格个数，对该文件夹下所有的csv文件进行操作
def get_time_relativity(path, max_x, min_x, max_y, min_y, grid_num):
    np.set_printoptions(suppress=True, formatter={'float_kind':'{:0.2f}'.format})
    # 得到大矩形的四个顶点的坐标
    p0 = [min_x, min_y]     # 左下
    p1 = [min_x, max_y]     # 左上
    p2 = [max_x, max_y]     # 右上
    p3 = [max_x, min_y]     # 右下
    # 把大矩形分成grid_num*grid_num个网格
    grid_x = np.linspace(min_x, max_x, grid_num)
    grid_y = np.linspace(min_y, max_y, grid_num)
    sum_check = 0
    # 把网格大小由经纬度转换为米
    print("栅格个数为：", grid_num, "栅格大小为：", (max_x - min_x)/grid_num, "*", (max_y - min_y)/grid_num, "经纬度")
    # 每6个小时的自行车需求量
    demand_num = []
    return_num = []
    # 循环遍历所有的csv文件
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        data = pd.read_csv(file_path)
        start_location = data['start_location']
        end_location = data['end_location']
        # 得到data中的start_location_x,y和end_location_x,y
        start_x = data['start_location_x']
        start_y = data['start_location_y']
        end_x = data['end_location_x']
        end_y = data['end_location_y']
        # 新建一个int型的矩阵，大小为grid_num*grid_num
        matrix = np.zeros((grid_num, grid_num), dtype=int)
        # 把start_location映射到网格上，每有一个网格上被映射，网格所对应的矩阵的值加1
        for i in range(len(start_location)):
            x = (int)((start_x[i] - min_x) / (max_x - min_x) * grid_num)
            y = (int)((start_y[i] - min_y) / (max_y - min_y) * grid_num)
            matrix[x-3][y-3] += 1
        if(matrix.sum() == len(start_location)):
            print("ok!", matrix.sum(), len(start_location))
            sum_check += matrix.sum()
            demand_num.append(matrix.sum())
        else:
            print("error!", matrix.sum(), len(start_location))
            break
        # 新建一个int型的矩阵，大小为grid_num*grid_num
        matrix = np.zeros((grid_num, grid_num), dtype=int)
        # 把end_location映射到网格上，每有一个网格上被映射，网格所对应的矩阵的值加1
        for i in range(len(start_location)):
            x = (int)((end_x[i] - min_x) / (max_x - min_x) * grid_num)
            y = (int)((end_y[i] - min_y) / (max_y - min_y) * grid_num)
            matrix[x-3][y-3] += 1
        if(matrix.sum() == len(end_location)):
            print("ok!", len(start_location))
            sum_check += matrix.sum()
            return_num.append(matrix.sum())
        else:
            print("error!", matrix.sum(), len(start_location))
            break
    print('sum_check:', sum_check)
    return demand_num, return_num, grid_x, grid_y

# # 传入为文件夹路径，location_x,y的最大最小值，网格个数，对该文件夹下所有的csv文件进行操作
def get_target_matrix(path, max_x, min_x, max_y, min_y, grid_num):
    np.set_printoptions(suppress=True,formatter={'float_kind':'{:0.2f}'.format})
    # 得到大矩形的四个顶点的坐标
    p0 = [min_x, min_y]     # 左下
    p1 = [min_x, max_y]     # 左上
    p2 = [max_x, max_y]     # 右上
    p3 = [max_x, min_y]     # 右下
    # 把大矩形分成grid_num*grid_num个网格
    grid_x = np.linspace(min_x, max_x, grid_num)
    grid_y = np.linspace(min_y, max_y, grid_num)
    sum_check = 0
    print("栅格个数为：", grid_num, "栅格大小为：", (max_x - min_x) / grid_num, "*", (max_y - min_y) / grid_num, "经纬度")
    # 每6个小时的自行车需求量
    demand_num = []
    return_num = []
    count = 0
    # 循环遍历所有的csv文件
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        data = pd.read_csv(file_path)
        start_location = data['start_location']
        end_location = data['end_location']
        # 得到data中的start_location_x,y和end_location_x,y
        start_x = data['start_location_x']
        start_y = data['start_location_y']
        end_x = data['end_location_x']
        end_y = data['end_location_y']
        # 新建一个int型的矩阵，大小为grid_num*grid_num
        matrix = np.zeros((grid_num, grid_num), dtype=int)
        # 把start_location映射到网格上，每有一个网格上被映射，网格所对应的矩阵的值加1
        for i in range(len(start_location)):
            x = m.floor((start_x[i] - min_x) / (max_x - min_x) * grid_num)
            y = m.floor((start_y[i] - min_y) / (max_y - min_y) * grid_num)
            matrix[x-3][y-3] += 1
        if matrix.sum() == len(start_location):
            print("ok!", matrix.sum(), len(start_location))
            sum_check += matrix.sum()
            demand_num.append(matrix.sum())
        else:
            print("error!", matrix.sum(), len(start_location))
            break
        # 将矩阵保存到csv文件中
        np.savetxt('matrix_s_txt'+'/'+file.split('.')[0] + '_s.txt', matrix, delimiter=' ')
        # 新建一个int型的矩阵，大小为grid_num*grid_num
        matrix = np.zeros((grid_num, grid_num), dtype=int)
        # 把end_location映射到网格上，每有一个网格上被映射，网格所对应的矩阵的值加1
        for i in range(len(start_location)):
            x = m.floor((end_x[i] - min_x) / (max_x - min_x) * grid_num)
            y = m.floor((end_y[i] - min_y) / (max_y - min_y) * grid_num)
            matrix[x-3][y-3] += 1
        if matrix.sum() == len(end_location):
            print("ok!", matrix.sum(), len(start_location))
            sum_check += matrix.sum()
            return_num.append(matrix.sum())
        else:
            print("error!", matrix.sum(), len(start_location))
            break
        # 将矩阵保存到csv文件中
        # np.savetxt('matrix_e_txt'+'/'+file.split('.')[0] + '_e.txt', matrix, delimiter=' ')
        # 新建dataframe，有三列，分别是cell_id, time, demand
        df = pd.DataFrame(columns=['cell_id', 'time', 'demand'])
        # 把矩阵变成一维数组
        matrix = matrix.flatten()
        print(len(matrix))
        print('sum_check:', sum_check)
        # 把矩阵的值放到dataframe中
        df['cell_id'] = np.arange(1, len(matrix)+1)
        df['time'] = np.arange(1, len(matrix)+1)
        df['demand'] = matrix
        # 保存到csv文件中
        df.to_csv('data_list'+'/'+file.split('.')[0] + '.csv', index=False)
    return demand_num, return_num, grid_x, grid_y

# 主函数
if __name__  ==  '__main__':
    file_path = 'processed_data/data_day32.csv'
    data_in_time_range(file_path, '00:00', '06:00')
    data_in_time_range(file_path, '06:00', '12:00')
    data_in_time_range(file_path, '12:00', '18:00')
    data_in_time_range(file_path, '18:00', '23:59')
    os.remove(file_path)
